451B - Sort the Array - CodeForces Solution


implementation sortings *1300

Please click on ads to support us..

Python Code:

import sys
input=sys.stdin.readline
n=int(input())
l=list(map(int,input().split()))
a=sorted(l)
i=0
x=0
y=n-1
k=1
while i<n:
    if l[i]==a[i] and k==1:
        i=i+1
    elif l[i]==a[i] and k==0:
        i=i-1
    else:
        if k==1:
            x=i
            k=0
            i=n-1
        else:
            y=i
            break
s=l[x:min(y+1,n)]
v=l[:max(x,0)]+s[::-1]+l[min(y+1,n):]
if a==v :
    print("yes")
    print(x+1,y+1)
elif a==l:
    print("yes")
    print(1,1)
else:
    print("no")

C++ Code:

// Date: 2023-01-31 15:06:19
// Problem: B. Sort the Array
// Contest: Codeforces - Codeforces Round #258 (Div. 2)
// URL: https://codeforces.com/problemset/problem/451/B
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// --------By WIDA--------

#include <bits/stdc++.h>
using namespace std;
namespace WIDA {
	using LL = long long;
	using PII = pair<LL, LL>;
	using TII = tuple<LL, LL, LL>;
	#define FOR(i,a,b) for (int i = (int)(a); i <= (int)(b); i ++)
	#define FOR2(i,a,b) for (int i = (int)(a); i <= (int)(b); i += 2)
	#define FORD(i,b,a) for (int i = (int)(a); i >= (int)(b); i --)
	#define ALL(a) a.begin(), a.end()
	#define rALL(a) a.rbegin(), a.rend()
	#define pb push_back
	#define fi first
	#define se second
	#define sz size()
	const LL INF = 0x3f3f3f3f3f3f3f3f;
	template <class... Args> void _(Args... args) {
	    auto _ = [&](auto x) { cout << x << " "; };
	    cout << "--->"; int arr[] = {(_(args), 0)...};
	    cout << "\n";
	}
	template <class T> void _i(T args) {
	    cout << "{";
	    for (auto i : args) cout << i << ", "; cout << "}\n";
	}
	template <class T> void _ii(T args) {
	    cout << "{"; 
	    for (auto [i, j] : args) cout << i << " " << j << ", ";
	    cout << "}\n";
	}
	template <class... Args> void __(Args... args) {
	    auto _ = [&](auto x) { cout << x << " "; };
	    int arr[] = {(_(args), 0)...}; cout << "\n";
	}
	#define int long long // Zmod要关闭
	#define endl "\n" // 交互题要关闭
	const int MOD = 998244353; // 看清楚每道题的 MOD
	const int N = 1e6 + 7; // 二维数组要修改 vector<vector<int> > a(n, vector<int> (m, 0));
}
using namespace WIDA;


bool Solve() {
	int n; cin >> n;
	vector<int> a(n + 1);
	for (int i = 1; i <= n; ++ i) cin >> a[i];
	a.push_back(INF);
	
	int l = 1, r = 1;
	for (int i = 1; i <= n; ++ i) {
		if (a[i - 1] < a[i] && a[i] > a[i + 1]) l = i;
		else if (a[i - 1] > a[i] && a[i] < a[i + 1]) r = i;
	}
	reverse(a.begin() + l, a.begin() + r + 1);
	if (is_sorted(a.begin(), a.end())) cout << "yes\n" << l << " " << r;
	else cout << "no";
	
	return 0;
}
signed main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	
	int Task = 1;
	// cin >> Task;
	for (int i = 1; i <= Task; ++ i) {
		Solve();
		// cout << (Solve() ? "YES" : "NO") << endl;
	}
	
	return 0;
}


Comments

Submit
0 Comments
More Questions

779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II